﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class EditPoster : System.Web.UI.UserControl
    {
        public string imagesposter;
        public string nameposter;
        public string link;
        public int posterid;
        public string filePathSlide;
        protected void Page_Load(object sender, EventArgs e)
        {
            posterid = Convert.ToInt32(Request.QueryString["idposter"]);
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Poster",posterid.ToString());
            imagesposter = dt.Rows[0]["Poster_Image"].ToString();
            link = dt.Rows[0]["Poster_Link"].ToString();
            nameposter = dt.Rows[0]["Poster_Name"].ToString();
            txtLink.Text = link;
            txtName.Text = nameposter;

        }

        protected void btnUpdatePoster_Click(object sender, EventArgs e)
        {
            if (txtLink.Text != "")
            {

                if (fulImagePoster.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImagePoster.FileName))
                    {
                        if (imagesposter != "")
                        {
                            string linkimage = fulImagePoster.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Poster/"), linkimage, 940, fulImagePoster.PostedFile.InputStream);
                            filePathSlide = "/Images/Image-Poster/" + fulImagePoster.FileName;
                            File.Delete(MapPath(imagesposter));
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                            cl[0] = new DAL.DHelpers.Column("Poster_Name", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Poster_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[2] = new DAL.DHelpers.Column("Poster_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string colWhere =  "Poster_ID";

                            string colValue = posterid.ToString();

                            DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Poster", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=5&sub=4");
                        }
                        else
                        {
                            string linkimage = fulImagePoster.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Poster/"), linkimage, 940, fulImagePoster.PostedFile.InputStream);
                            filePathSlide = "/Images/Image-Poster/" + fulImagePoster.FileName;
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                            cl[0] = new DAL.DHelpers.Column("Poster_Name", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Poster_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[2] = new DAL.DHelpers.Column("Poster_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string colWhere = "Poster_ID";

                            string colValue = posterid.ToString();

                            DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Poster", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=5&sub=4");
                        }


                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }

                }
                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[2];
                    cl[0] = new DAL.DHelpers.Column("Poster_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[1] = new DAL.DHelpers.Column("Poster_Name", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    

                    string colWhere = "Poster_ID";

                    string colValue = posterid.ToString();

                    DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Poster", cl, colWhere, colValue);

                    Response.Redirect("/Admin/Index.aspx?panel=5&sub=4");
                }

            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập link liên kết...')</script>");
            }
        }
    }
}